package com.itheima.work;


import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @auther Macrydy
 * @date 2020/7/3 19:05
 * 编程题【综合】
 * 1.	定义一个方法,该方法可以完成键盘录入学生对象到集合里.并且把集合中的数据写到文件上.
 * 2.	定义一个方法,该方法可以完成把文件上的学生对象数据读取到集合中.然后遍历集合打印学生数据.
 * 3.	在main方法中调用以上两个方法.
 */
public class Test9 {
    public static void main(String[] args) throws IOException {
        method1();
//        meyhod2();
    }

    private static void meyhod2() throws IOException {
        List<Student> stuList2 = new ArrayList<>();
        FileInputStream fis=new FileInputStream("day10IO\\student1.txt");
        byte[] bt=new byte[1024];
        int len;
        String s=null;
        while ((len=fis.read(bt))!=-1){
            s = new String(bt, 0, len);

        }

        String[] split = s.split("\r\n");
        for (String s1 : split) {
            String[] split1 = s1.split(",");

            stuList2.add(new Student(split1[0],split1[1],Integer.parseInt(split1[2]),Integer.parseInt(split1[3])));
        }

        for (Student student : stuList2) {
            System.out.println(student);
        }
    }

    public static void method1() throws IOException {
        Scanner sc = new Scanner(System.in);
        List<Student> stuList = new ArrayList<>();

        while (true){
            System.out.println("请输入姓名：");
            String name = sc.nextLine();
            System.out.println("请输入性别：");
            String sex = sc.nextLine();
            System.out.println("请输入年龄：");
            String age = sc.nextLine();
            System.out.println("请输入分数：");
            String score = sc.nextLine();

            stuList.add(new Student(name, sex, Integer.parseInt(age), Integer.parseInt(score)));

            System.out.println("是否继续输入：");
            String s = sc.nextLine();
            if (s.equals("否")){
                break;
            }

        }


        FileOutputStream fos = new FileOutputStream("day10IO\\student1.txt");
        for (Student student : stuList) {
            fos.write(student.getName().getBytes());
            fos.write(",".getBytes());
            fos.write(student.getSex().getBytes());
            fos.write(",".getBytes());
            fos.write((student.getAge() + "").getBytes());
            fos.write(",".getBytes());
            fos.write((student.getScore() + "").getBytes());
            fos.write("\r\n".getBytes());
        }
    }


}
